package main.niit.dao.impl;

import main.niit.dao.SemesterDao;
import main.niit.pojo.Semester;
import main.niit.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class SemesterDaoImpl implements SemesterDao {
    @Override
    public List<Semester> getgendersemester() throws Exception {
        Connection connection=JDBCUtils.getConnection();
        List<Semester> list2=new ArrayList<>();
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try {

            String sql = "SELECT semester, `1man`, `0woman` FROM gendersemester ORDER BY semester;";
            ps = connection.prepareStatement(sql);
            resultSet = ps.executeQuery();
            while (resultSet.next()) {
                Semester semester = new Semester();
                semester.setSemester(resultSet.getInt("Semester"));
                semester.setMan(resultSet.getInt("1man"));
                semester.setWoman(resultSet.getInt("0woman"));


                list2.add(semester);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JDBCUtils.close(ps,connection);
        }

        return list2;

    }
}
